<template>
  <div class="user-list-container">
    <!-- 表格 -->
    <el-card shadow="never" class="table-card">
      <div style="display: flex; margin-bottom: 20px">
        <el-button type="primary" @click="openAddDialog">创建分类</el-button>
      </div>
      <el-table
        v-loading="loading"
        :data="tableData"
        border
        class="table"
      >
        <el-table-column prop="activity_category_id" label="ID" width="80" />
        <el-table-column prop="activity_category_name" label="分类名称" />
        <el-table-column prop="is_show" label="是否显示">
          <template #default="scope">
            <el-switch
              v-model="scope.row.is_show"
              :active-value="1"
              :inactive-value="0"
              @change="handleSwitchChange(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column prop="created_at" label="创建时间" />
        <el-table-column prop="updated_at" label="修改时间" />
        <el-table-column label="操作" width="280">
          <template #default="scope">
            <el-button link @click="handleTableOption('edit', scope.row)"
              >编辑</el-button
            >
            <el-button link type="danger" @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        class="pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      />
    </el-card>

    <!-- 创建/编辑分类对话框 -->
    <el-dialog
      :title="dialogType === 'edit' ? '编辑分类' : '创建分类'"
      v-model="dialogVisible"
      width="500px"
      @close="handleCloseAddDialog"
    >
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="100px"
        label-position="right"
      >
        <el-form-item label="分类名称" prop="activity_category_name">
          <el-input
            v-model="form.activity_category_name"
            placeholder="请输入分类名称"
          />
        </el-form-item>
        <el-form-item label="是否显示" prop="is_show">
          <el-switch
            v-model="form.is_show"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="handleCloseAddDialog">取消</el-button>
          <el-button type="primary" @click="submitForm">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import tableMixin from "@/mixins/tableMixin";
import api from "@/request/api.js";

export default {
  name: "UserList",
  mixins: [tableMixin],
  data() {
    return {
      //查询参数
      queryParams: {},
      apimodel: "classaa",
      apival: "getList",
      //创建修改弹窗
      dialogVisible: false,
      dialogType: "",
      form: {
        activity_category_name: "",
        is_show: 1,
      },
      rules: {
        activity_category_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {
    this.getTableData();
  },
  methods: {
    // 切换显示状态
    async handleSwitchChange(row) {
      try {
        const res = await api.classaa.updates({
          activity_category_id: row.activity_category_id,
          is_show: row.is_show,
        });
        if (res?.status === 200) {
          this.$message.success("状态更新成功");
          this.getTableData();
        }
      } catch (error) {
        this.$message.error("状态更新失败");
        // 恢复原来的状态
        row.is_show = row.is_show === 1 ? 0 : 1;
      }
    },

    openAddDialog(option = "") {
      this.dialogVisible = true;
      this.dialogType = option;
    },

    handleCloseAddDialog() {
      this.dialogVisible = false;
      this.dialogType = "";
      this.form = {
        activity_category_name: "",
        is_show: 1,
      };
      this.$refs.formRef?.resetFields();
    },

    // 编辑
    handleTableOption(option, row) {
      if (option === "edit") {
        this.form = {
          activity_category_id: row.activity_category_id,
          activity_category_name: row.activity_category_name,
          is_show: row.is_show,
        };
        this.openAddDialog(option);
      }
    },

    // 删除
    handleDelete(row) {
      this.$confirm("确认删除该分类吗?", "提示", {
        type: "warning",
      })
        .then(async () => {
          const res = await api.classaa.del({
            activity_category_id: row.activity_category_id,
          });
          if (res?.status === 200) {
            this.$message.success("删除成功");
            this.getTableData();
          }
        })
        .catch(() => {});
    },

    // 提交表单
    submitForm() {
      this.$refs.formRef.validate(async (valid) => {
        if (valid) {
          try {
            let res;
            if (this.dialogType !== "edit") {
              // 创建
              res = await api.classaa.create({ ...this.form });
              if (res?.status === 200) {
                this.$message.success("创建成功");
              }
            } else {
              // 编辑
              res = await api.classaa.update({ ...this.form });
              if (res?.status === 200) {
                this.$message.success("修改成功");
              }
            }
            this.handleCloseAddDialog();
            this.getTableData();
          } catch (error) {
            this.$message.error("操作失败");
          }
        }
      });
    },
  },
};
</script>

<style scoped>
.user-list-container {
  padding: 20px;
}
.search-card {
  margin-bottom: 20px;
}
.table-card {
  margin-bottom: 20px;
}
.pagination {
  margin-top: 20px;
  text-align: right;
}
</style>